//==============================================================================
//
//               Copyright (C) 2002  Dick van Oudheusden
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2003-10-31 08:12:35 $ $Revision: 1.1 $
//
//==============================================================================

#include <stdlib.h>

#include "ofc/config.h"

#include "ofc/DText.h"

#include "DInc.h"
#include "DTest.h"

//-DataTypes-------------------------------------------------------------------

void DText_test()
{
  DText *string = [DText alloc];
  DText *copy;
  char   ch;
  
  STARTTEST();

  [[string init] format :"Hello world %d",4];
  
  TEST([string ccompare :"hello world 4"] != 0);
  TEST([string ccompare :"Hello world 4"] == 0);
  TEST([string icompare :"hello world 4"] == 0);
  TEST([string icompare :"hello world 5"] != 0);

  ch = [string pop];

  TEST((ch == '4') && ([string ccompare :"Hello world "] == 0) && ([string length] == 12));

  [string free];
  string = [DText alloc];
  
  string = [[string init :"Hello"] center :7];

  TEST([string ccompare :" Hello "] == 0);

  [string delete :6];
  [string put    :4 :'?'];
  ch = [string get :5];

  TEST((ch == 'o') && ([string ccompare :" Hel?o"] == 0));

  copy = [string copy];
  
  // Copy test
  TEST(string != copy);
  TEST([string cstring] != [copy cstring]);
  TEST([string compare :copy] == 0);

  [copy free];

  // Modifiers test
  TEST([[string append :" DAAG"] ccompare :" Hel?o DAAG"] == 0); 
  TEST([[string swapcase] ccompare :" hEL?O daag"] == 0);
  TEST([[string lower] ccompare :" hel?o daag"] == 0);
  TEST([[string prepend :"test"] ccompare :"test hel?o daag"] == 0);
  TEST([[string capitalize] ccompare :"Test hel?o daag"] == 0);
  TEST([[string capwords] ccompare :"Test Hel?o Daag"] == 0);

  [string free];

  // Range tests  
  string = [DText alloc];
  
  [string init];
  [string set :"This is a long text text" :0 :18];

  TEST([string ccompare :"This is a long text"] == 0);

  TEST([[string insert :15 :18 :"sentence"] ccompare :"This is a long sentence"] == 0);
  TEST([[string insert :0 :3 :"1."] ccompare :"1. is a long sentence"] == 0); 

  TEST([[string get :1 :-2] ccompare :". is a long sentenc"] == 0); 

  TEST([[string delete :0 :1] ccompare :" is a long sentence"] == 0);
  TEST([[string delete :11 :18] ccompare :" is a long "] == 0);

  [string set :""];

  TEST([string isEof]);
  TEST([string writeLine :"Hallo"]);
  TEST([string ccompare :"Hallo\n"] == 0);
  TEST([string isEof]);
  TEST([string tell] == 6);
  TEST([string writeChar :'1']);
  TEST([string ccompare :"Hallo\n1"] == 0);
  TEST([string writeText :";"]);
  TEST([string ccompare :"Hallo\n1;"] == 0);

  TEST([string seek :3 :2]);
  TEST([string tell] == 5);
  TEST([string readChar] == '\n');
  TEST([string seek :1 :0]);
  TEST([[string readLine] ccompare :"allo"] == 0);
  TEST([[string readText :1] ccompare :"1"] == 0);
  TEST([string seek :0 :0]);
  TEST([[string readText] ccompare :"Hallo\n1;"] == 0);
  
  [string free];
  
  STOPTEST();
}

